//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "OISRateHelper.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Indexes/OvernightIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Instruments/OvernightIndexedSwap.h>
#include <gen/QL/Termstructures/Yield/RelativeDateRateHelper.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::COISRateHelper::COISRateHelper (UInt32 settlementDays, Cephei::QL::Times::IPeriod^ tenor, Cephei::QL::IQuote^ fixedRate, Cephei::QL::Indexes::IOvernightIndex^ overnightIndex) : CRelativeDateRateHelper(COISRateHelper::typeid)
{
    CPeriod^ _Ctenor;
    CQuote^ _CfixedRate;
    COvernightIndex^ _CovernightIndex;
    try
    {
#ifdef HANDLE
        _phOISRateHelper = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays);
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        _CfixedRate = safe_cast<CQuote^> (fixedRate);
        _CfixedRate->Lock();
        Handle<QuantLib::Quote>& _fixedRate = static_cast<Handle<QuantLib::Quote>&> (_CfixedRate->GetHandle ()); 
        _CovernightIndex = safe_cast<COvernightIndex^> (overnightIndex);
        _CovernightIndex->Lock();
        boost::shared_ptr<QuantLib::OvernightIndex>& _overnightIndex = static_cast<boost::shared_ptr<QuantLib::OvernightIndex>&> (_CovernightIndex->GetShared ()); 
        _ppOISRateHelper = new boost::shared_ptr<QuantLib::OISRateHelper> (new QuantLib::OISRateHelper ( _settlementDays,  _tenor,  _fixedRate,  _overnightIndex ));
        SetRelativeDateRateHelper (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppOISRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_CfixedRate != nullptr) _CfixedRate->Unlock();
        if (_CovernightIndex != nullptr) _CovernightIndex->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::COISRateHelper::COISRateHelper (boost::shared_ptr<QuantLib::OISRateHelper>& childNative, Object^ owner) : CRelativeDateRateHelper(COISRateHelper::typeid)
{
#ifdef HANDLE
	_phOISRateHelper = NULL;
#endif
	_ppOISRateHelper = &childNative;
    _ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppOISRateHelper));
}
Cephei::QL::Termstructures::Yield::COISRateHelper::COISRateHelper (QuantLib::OISRateHelper& childNative, Object^ owner) : CRelativeDateRateHelper(COISRateHelper::typeid)
{
#ifdef HANDLE
	_phOISRateHelper = NULL;
#endif
	_ppOISRateHelper = new boost::shared_ptr<QuantLib::OISRateHelper> (&childNative);
    _ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppOISRateHelper));
    _OISRateHelperOwner = owner;
    _RelativeDateRateHelperOwner = owner;
}

Cephei::QL::Termstructures::Yield::COISRateHelper::COISRateHelper (COISRateHelper^ copy) : CRelativeDateRateHelper(COISRateHelper::typeid)
{
#ifdef HANDLE
	_phOISRateHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOISRateHelper = new boost::shared_ptr<QuantLib::OISRateHelper> (copy->GetShared());
        _ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppOISRateHelper));
    }
}
Cephei::QL::Termstructures::Yield::COISRateHelper::COISRateHelper (System::Type^ t) : CRelativeDateRateHelper(COISRateHelper::typeid)
{
#ifdef HANDLE
	_phOISRateHelper = NULL;
#endif
	if (!t->IsSubclassOf(COISRateHelper::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::COISRateHelper::COISRateHelper (QuantLib::Handle<QuantLib::OISRateHelper>& childNative, Object^ owner)  : CRelativeDateRateHelper(COISRateHelper::typeid)
{
	_phOISRateHelper = &childNative;
	_ppOISRateHelper = &static_cast<boost::shared_ptr<QuantLib::OISRateHelper>>(childNative.currentLink());
    _ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppOISRateHelper));
    _OISRateHelperOwner = owner;
}
Cephei::QL::Termstructures::Yield::COISRateHelper::COISRateHelper (QuantLib::Handle<QuantLib::OISRateHelper> childNative)  : CRelativeDateRateHelper(COISRateHelper::typeid)
{
	_phOISRateHelper = &childNative;
	_ppOISRateHelper = &static_cast<boost::shared_ptr<QuantLib::OISRateHelper>>(childNative.currentLink());
    _ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppOISRateHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::COISRateHelper::COISRateHelper (QuantLib::OISRateHelper childNative)  : CRelativeDateRateHelper(COISRateHelper::typeid)
{
#ifdef HANDLE
	_phOISRateHelper = NULL;
#endif
	_ppOISRateHelper = new boost::shared_ptr<QuantLib::OISRateHelper> (new QuantLib::OISRateHelper (childNative));
    _ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppOISRateHelper));
}
#endif

Cephei::QL::Termstructures::Yield::COISRateHelper::~COISRateHelper ()
{
    if (_ppOISRateHelper != NULL)
    {
	    delete _ppOISRateHelper;
        _ppOISRateHelper = NULL;
    }
}
Cephei::QL::Termstructures::Yield::COISRateHelper::!COISRateHelper ()
{
    if (_ppOISRateHelper != NULL)
    {
	    delete _ppOISRateHelper;
    }
}
QuantLib::OISRateHelper& Cephei::QL::Termstructures::Yield::COISRateHelper::GetReference ()
{
    if (_ppOISRateHelper == NULL) throw gcnew NativeNullException ();
	return **_ppOISRateHelper;
}
boost::shared_ptr<QuantLib::OISRateHelper>& Cephei::QL::Termstructures::Yield::COISRateHelper::GetShared ()
{
    if (_ppOISRateHelper == NULL) throw gcnew NativeNullException ();
	return *_ppOISRateHelper;
}
QuantLib::OISRateHelper* Cephei::QL::Termstructures::Yield::COISRateHelper::GetPointer ()
{
    if (_ppOISRateHelper == NULL) throw gcnew NativeNullException ();
	return &**_ppOISRateHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OISRateHelper>& Cephei::QL::Termstructures::Yield::COISRateHelper::GetHandle ()
{
	if (_phOISRateHelper == NULL)
	{
		_phOISRateHelper = new Handle<QuantLib::OISRateHelper> (*_ppOISRateHelper);
	}
	return *_phOISRateHelper;
}
#endif
bool Cephei::QL::Termstructures::Yield::COISRateHelper::HasNative () 
{
	return (_ppOISRateHelper != NULL);
}

Double Cephei::QL::Termstructures::Yield::COISRateHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOISRateHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Yield::IOISRateHelper^ Cephei::QL::Termstructures::Yield::COISRateHelper::SetTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ t)
{
    CYieldTermStructure^ _Ct;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ct = safe_cast<CYieldTermStructure^> (t);
        _Ct->Lock();
        QuantLib::YieldTermStructure* _t = static_cast<QuantLib::YieldTermStructure*> (_Ct->GetPointer ()); 
    	(*_ppOISRateHelper)->setTermStructure ( _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ct != nullptr) _Ct->Unlock();
    }
}
Cephei::QL::Instruments::IOvernightIndexedSwap^ Cephei::QL::Termstructures::Yield::COISRateHelper::Swap::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::OvernightIndexedSwap> _rv = (boost::shared_ptr<QuantLib::OvernightIndexedSwap>)(*_ppOISRateHelper)->swap ( );   
        Cephei::QL::Instruments::COvernightIndexedSwap^ _nrv = gcnew Cephei::QL::Instruments::COvernightIndexedSwap (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IOISRateHelper^ Cephei::QL::Termstructures::Yield::COISRateHelper_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::IPeriod^ tenor, Cephei::QL::IQuote^ fixedRate, Cephei::QL::Indexes::IOvernightIndex^ overnightIndex)
{
    return gcnew COISRateHelper ( settlementDays,  tenor,  fixedRate,  overnightIndex);
}
